package com.ninong.ker.gateway.auth;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * 签名工具类
 * @author 鸠摩智
 * @date 2020年12月10日 下午2:17:40
 */
public class Signutil {

    static final String salt = "Khaos-hc-2020%^$*&#";

    /**
     * @Description 接口安全性校验
     * @Params [executeTime, encrypt]
     */
    public static boolean interfaceSecurityAuth(Long executeTime, String encrypt,String url) {
        //获取2秒之前的时间
        LocalDateTime min = LocalDateTime.now().minusSeconds(1);
        LocalDateTime time = LocalDateTime.ofEpochSecond(executeTime/1000,0, ZoneOffset.ofHours(8));
        if (time.compareTo(min) >= 0 && time.compareTo(LocalDateTime.now()) < 0) {
            String md5Str = DigestUtils.md5Hex(salt + executeTime + url).toUpperCase();
            if (md5Str.equals(encrypt)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}
